package com.cyrus.vueboot.controller.auth;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cyrus.vueboot.common.R;
import com.cyrus.vueboot.error.BusinessError;
import com.cyrus.vueboot.error.CommonError;
import com.cyrus.vueboot.pojo.auth.Admin;
import com.cyrus.vueboot.service.AdminService;
import com.cyrus.vueboot.divAnno.OpsLogAnnotation;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: Cyrus
 * @CreateDate: 2020/11/14
 */

@Slf4j
@Api(value = "权限用户服务的Controller", tags = "Admin", description = "权限用户服务API")
@RestController
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private AdminService adminService;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @ApiOperation("请求所有用户数据")
    @ApiImplicitParams({@ApiImplicitParam(name="page",value = "当前页",required=true,dataType="Integer"),
                        @ApiImplicitParam(name="size",value = "展示数据量",required=true,dataType="Integer"),
                         @ApiImplicitParam(name="key",value = "查询关键字",required=false,dataType="String")})
    @PostMapping("/search")
    public R index(Integer page, Integer size, String key) {
        IPage<Admin> pages = adminService.selectAll(page, size,key);
        List<Admin> records = pages.getRecords();
        long total = pages.getTotal();
        long size1 = pages.getSize();
        return R.ok().data("records", records).data("total", total).data("size",size1);
    }

    @ApiOperation("删除指定用户")
    @ApiImplicitParam(name="ids",value = "需要删除的用户id集合",required=true,dataType="Long[]")
    @PostMapping("/deleteAdmins")
    @OpsLogAnnotation(module = "权限管理",description = "删除用户")
    public R deleteAdmins(Long[] ids) {
        try {
            if (ids != null) {
                for (Long id : ids) {
                    adminService.deleteAdmins(id);
                }
            }
            return R.ok();
        } catch (Exception e) {
            throw new BusinessError(CommonError.E_10002.getCode(), CommonError.E_10002.getMsg());
        }
    }

    @ApiOperation("回显指定用户数据")
    @ApiImplicitParam(name="id",value = "需要回显的用户id",required=true,dataType="Integer")
    @GetMapping("/findById")
    @OpsLogAnnotation(module = "权限管理",description = "获取用户信息")
    public R findById(Integer id) {
        Admin admin = adminService.getById(id);

        return R.ok().data("item",admin);
    }

    @ApiOperation("添加用户数据")
    @PostMapping("/add")
    @ApiImplicitParam(name="admin",value = "添加的用户信息",required=true,dataType="Admin")
    @OpsLogAnnotation(module = "权限管理",description = "添加用户信息")
    public R add(@RequestBody Admin admin) {
        try {
            admin.setStatus(1);
            admin.setIsShow(1);
            admin.setPassword(passwordEncoder.encode(admin.getPassword()));

            admin.setAuthority("普通员工");
            adminService.save(admin);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessError(CommonError.E_10001.getCode(), CommonError.E_10001.getMsg());
        }
    }

    @ApiOperation("更新指定用户数据")
    @ApiImplicitParam(name="admin",value = "更新的用户信息",required=true,dataType="Admin")
    @PostMapping("/update")
    @OpsLogAnnotation(module = "权限管理",description = "更新用户信息")
    public R update(@RequestBody Admin admin) {
        try {
            admin.setPassword(passwordEncoder.encode(admin.getPassword()));
            adminService.updateById(admin);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessError(CommonError.E_10003.getCode(), CommonError.E_10003.getMsg());
        }
    }

    @ApiOperation("更新指定用户的登录权限")
    @ApiImplicitParam(name="id",value = "需要修改登录权限的用户id",required=true,dataType="Integer")
    @PostMapping("/changeStatus")
    @OpsLogAnnotation(module = "权限管理",description = "切换用户身份信息")
    public R changeStatus(Integer id) {
        try {
            Admin admin = adminService.getById(id);
            if (admin.getStatus() == 1) {
                admin.setStatus(0);
            } else {
                admin.setStatus(1);
            }
            adminService.updateById(admin);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessError(CommonError.E_10003.getCode(), CommonError.E_10003.getMsg());
        }
    }
}
